/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestagric;
import java.net.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author Riou
 */
public class Contact_Client extends javax.swing.JFrame {
    /**
     * Creates new form Contact_Client
     */
    public Contact_Client() {
                
        initComponents();
                
        String pilote = "org.gjt.mm.mysql.Driver";
        String url = new String("jdbc:mysql://ks3262762.kimsufi.com/c3web2do");			        
	try
	{
            Class.forName(pilote);           			            
            Connection conn = DriverManager.getConnection(url,"c3web2do","wxpNb9JRUJv3HFpU");
            Statement stmt = conn.createStatement();			            
            ResultSet rs = stmt.executeQuery("select * from categories");			            
            while (rs.next())
            {
                    jcmbTypeQuestion.addItem(rs.getString("lib_cat"));
            }           			            
            rs.close();
            stmt.close();
            conn.close();
        }			        
	catch (SQLException sqle)
	{
            System.out.println(sqle.getMessage());
	}
	catch (ClassNotFoundException cnfe)
	{
            System.out.println(cnfe.getMessage());
	}
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlblTypeQestion = new javax.swing.JLabel();
        jcmbTypeQuestion = new javax.swing.JComboBox();
        jlblQuestion = new javax.swing.JLabel();
        jtxtQuestion = new javax.swing.JTextField();
        jbtnEnvoiQuestion = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jlblReponse = new javax.swing.JLabel();
        jtxtReponse = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jlblTypeQestion.setText("Type de question :");

        jlblQuestion.setText("Question :");

        jtxtQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtQuestionActionPerformed(evt);
            }
        });

        jbtnEnvoiQuestion.setText("Envoi Question");
        jbtnEnvoiQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEnvoiQuestionActionPerformed(evt);
            }
        });

        jlblReponse.setText("Réponse :");

        jtxtReponse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtReponseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnEnvoiQuestion)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblReponse)
                    .addComponent(jlblQuestion)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlblTypeQestion)
                        .addGap(18, 18, 18)
                        .addComponent(jcmbTypeQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtReponse, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblTypeQestion)
                    .addComponent(jcmbTypeQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addComponent(jlblQuestion)
                .addGap(18, 18, 18)
                .addComponent(jtxtQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnEnvoiQuestion)
                .addGap(27, 27, 27)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jlblReponse)
                .addGap(18, 18, 18)
                .addComponent(jtxtReponse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnEnvoiQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEnvoiQuestionActionPerformed
        try{
            String question, reponse;
            Socket socket = new Socket("192.168.1.61", 1995);
            System.out.println("SOCKET = "+socket);

            BufferedReader plec = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter pred = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

            //Envoi de la question
            question = jtxtQuestion.getText();
            pred.println(question);
            reponse = plec.readLine();
            jtxtReponse.setText(reponse);
                
            pred.println("END");
            plec.close();
            pred.close();
            socket.close();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_jbtnEnvoiQuestionActionPerformed

    private void jtxtReponseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtReponseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtReponseActionPerformed

    private void jtxtQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtQuestionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtQuestionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception  {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Contact_Client().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtnEnvoiQuestion;
    private javax.swing.JComboBox jcmbTypeQuestion;
    private javax.swing.JLabel jlblQuestion;
    private javax.swing.JLabel jlblReponse;
    private javax.swing.JLabel jlblTypeQestion;
    private javax.swing.JTextField jtxtQuestion;
    private javax.swing.JTextField jtxtReponse;
    // End of variables declaration//GEN-END:variables
}
